<html>
 <head>
  <meta charset="UTF-8">
 </head>
 <body>
  <h1 data-lake-id="HftOs" id="HftOs"><span data-lake-id="u5db69b96" id="u5db69b96">典型回答</span></h1>
  <p data-lake-id="u1cf87eaf" id="u1cf87eaf"><br></p>
  <p data-lake-id="ue7aa0d81" id="ue7aa0d81"><span data-lake-id="ud0a7e022" id="ud0a7e022">很多人在读源码的时候，经常可以看到以下代码：</span></p>
  <p data-lake-id="u22f5a833" id="u22f5a833"><br></p>
  <p data-lake-id="u05ba5afb" id="u05ba5afb"><span data-lake-id="u02113fc4" id="u02113fc4">Spring中：</span></p>
  <p data-lake-id="u9c88733c" id="u9c88733c"><img src="https://cdn.nlark.com/yuque/0/2023/png/5378072/1699169900674-ea85e8aa-ae76-4fda-b516-ff6350b1be64.png?x-oss-process=image%2Fwatermark%2Ctype_d3F5LW1pY3JvaGVp%2Csize_59%2Ctext_SmF2YSA4IEd1IFA%3D%2Ccolor_FFFFFF%2Cshadow_50%2Ct_80%2Cg_se%2Cx_10%2Cy_10"></p>
  <p data-lake-id="u5d6f7053" id="u5d6f7053"><br></p>
  <p data-lake-id="ua5c5418e" id="ua5c5418e"><span data-lake-id="u5d495d49" id="u5d495d49">Dubbo中：</span></p>
  <p data-lake-id="ua9a4d043" id="ua9a4d043"><img src="https://cdn.nlark.com/yuque/0/2023/png/5378072/1699169943116-7eb05894-f704-4b33-ac40-f5973844595a.png?x-oss-process=image%2Fwatermark%2Ctype_d3F5LW1pY3JvaGVp%2Csize_59%2Ctext_SmF2YSA4IEd1IFA%3D%2Ccolor_FFFFFF%2Cshadow_50%2Ct_80%2Cg_se%2Cx_10%2Cy_10"></p>
  <p data-lake-id="u8bf06bc4" id="u8bf06bc4"><br></p>
  <p data-lake-id="ua2b5c251" id="ua2b5c251"><span data-lake-id="u07bc5314" id="u07bc5314">也就是说，很多框架，在执行warn()、debug()等方法前，都会额外的调用一下isWarnEnabled()和isDebugEnabled()等方法，这是为什么呢？</span></p>
  <p data-lake-id="u0633f54a" id="u0633f54a"><span data-lake-id="u12cc1043" id="u12cc1043">​</span><br></p>
  <p data-lake-id="u167f778e" id="u167f778e"><span data-lake-id="u6dff277d" id="u6dff277d">isWarnEnabled、isDebugEnabled等方法，其实是判断当前日志级别是否开启的，如果开启则返回true，否则返回false。</span></p>
  <p data-lake-id="u081bd822" id="u081bd822"><span data-lake-id="ud4e01751" id="ud4e01751">​</span><br></p>
  <p data-lake-id="uf4145a01" id="uf4145a01"><span data-lake-id="uf46802b1" id="uf46802b1">其实，之所以要提前调用一次isWarnEnabled，主要是为了提升性能的，因为在记录日志时，生成日志消息的过程可能会涉及方法的执行、字符串的拼接、对象的序列化等操作。而这些操作都是比较耗费时间的。</span></p>
  <p data-lake-id="u481c156d" id="u481c156d"><span data-lake-id="u6bf889ac" id="u6bf889ac">​</span><br></p>
  <p data-lake-id="ue0a59386" id="ue0a59386"><span data-lake-id="u29a43777" id="u29a43777">如果一段日志，不需要输出，那么这些步骤其实是可以省略的。</span></p>
  <p data-lake-id="ue6d1579a" id="ue6d1579a"><br></p>
  <p data-lake-id="uaf72a45d" id="uaf72a45d"><span data-lake-id="uf1ea1d52" id="uf1ea1d52">比如我们有以下日志要输出：</span></p>
  <p data-lake-id="u1b22a3bd" id="u1b22a3bd"><span data-lake-id="uec3ed325" id="uec3ed325">​</span><br></p>
  <pre lang="java"><code>
public void login(LoginRequest loginRequest){
    if (logger.isWarnEnabled()) {
        logger.warn("This is a message with: " + JSON.toJSONString(loginRequest));
    }
}
</code></pre>
  <p data-lake-id="udb8e8a38" id="udb8e8a38"><br></p>
  <p data-lake-id="u7f257f4a" id="u7f257f4a"><span data-lake-id="uad5a07a4" id="uad5a07a4">通过</span><code data-lake-id="ue2e9c194" id="ue2e9c194"><span data-lake-id="ubf50db43" id="ubf50db43">logger.isWarnEnabled</span></code><span data-lake-id="ufb48d389" id="ufb48d389">做一次前置判断，那么就可以在warn级别不生效时，能避免 </span><code data-lake-id="u295ec886" id="u295ec886"><span data-lake-id="uc5275cfe" id="uc5275cfe">JSON.toJSONString(loginRequest)</span></code><span data-lake-id="u11e0d157" id="u11e0d157">方法的执行，也能避免</span><code data-lake-id="u9127d9f8" id="u9127d9f8"><span data-lake-id="u527653b1" id="u527653b1">"This is a message with: " + JSON.toJSONString(loginRequest)</span></code><span data-lake-id="udf3fd997" id="udf3fd997">这个字符串拼接操作的执行。</span></p>
  <p data-lake-id="u0f390c6c" id="u0f390c6c"><span data-lake-id="u81abd8bc" id="u81abd8bc">​</span><br></p>
  <p data-lake-id="u6659c7af" id="u6659c7af"><span data-lake-id="u6ea1076c" id="u6ea1076c">所以，使用</span><code data-lake-id="u620c8513" id="u620c8513"><span data-lake-id="ue82a2944" id="ue82a2944">logger.isXxxEnabled()</span></code><span data-lake-id="uc94a3fc1" id="uc94a3fc1">用于检查日志Xxx级别是否启用，可以在需要时避免不必要的开销，提高应用程序的性能。这种做法特别在记录频繁的日志消息时，尤为重要。</span></p>
 </body>
</html>